package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	widgetinterface "clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/driver/desktop"
	"image"
	"image/color"
	"strconv"
	"strings"
)

// 注册控件
func init() {
	widget.RegWidget("WidgetEllipse", "椭圆", NewEllipse)
}

// Ellipse 椭圆控件
type Ellipse struct {
	*WidgetContainer
	*EllipseShape // 使用自定义椭圆形状
	*Action
	widgetinterface.IUserInfo
	rootNode widgetinterface.IWidgetItem

	// 椭圆属性
	Width       float32
	Height      float32
	BorderWidth float32
	FillColor   color.RGBA
	BorderColor color.RGBA
}

// EllipseShape 椭圆基础形状类
type EllipseShape struct {
	*canvas.Raster
	Width, Height float32
	FillColor     color.Color
	StrokeColor   color.Color
	StrokeWidth   float32
}

// NewEllipseShape 创建椭圆形状
func NewEllipseShape(fillColor color.Color, w, h float32) *EllipseShape {
	e := &EllipseShape{
		Width:       w,
		Height:      h,
		FillColor:   fillColor,
		StrokeColor: color.RGBA{0, 0, 0, 255},
		StrokeWidth: 1,
	}

	// 创建Raster用于自定义绘制
	e.Raster = canvas.NewRaster(e.drawEllipse)
	return e
}

// drawEllipse 绘制椭圆像素
func (e *EllipseShape) drawEllipse(w, h int) image.Image {
	img := image.NewRGBA(image.Rect(0, 0, w, h))

	// 椭圆参数 - 使用控件的实际尺寸
	centerX := float64(w) / 2
	centerY := float64(h) / 2
	radiusX := float64(e.Width) / 2
	radiusY := float64(e.Height) / 2

	// 填充椭圆
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			// 椭圆方程: (x-cx)²/a² + (y-cy)²/b² <= 1
			dx := float64(x) - centerX
			dy := float64(y) - centerY
			if (dx*dx)/(radiusX*radiusX)+(dy*dy)/(radiusY*radiusY) <= 1 {
				img.Set(x, y, e.FillColor)
			}
		}
	}

	return img
}

// MinSize 最小尺寸
func (e *EllipseShape) MinSize() fyne.Size {
	return fyne.NewSize(e.Width, e.Height)
}

// Resize 调整尺寸
func (e *EllipseShape) Resize(size fyne.Size) {
	e.Width = size.Width
	e.Height = size.Height
	e.Raster.Resize(size)
}

// Refresh 刷新
func (e *EllipseShape) Refresh() {
	e.Raster.Refresh()
}

// Move 移动
func (e *EllipseShape) Move(pos fyne.Position) {
	e.Raster.Move(pos)
}

// Position 获取位置
func (e *EllipseShape) Position() fyne.Position {
	return e.Raster.Position()
}

// Size 获取尺寸
func (e *EllipseShape) Size() fyne.Size {
	return e.Raster.Size()
}

// Visible 是否可见
func (e *EllipseShape) Visible() bool {
	return e.Raster.Visible()
}

// Show 显示
func (e *EllipseShape) Show() {
	e.Raster.Show()
}

// Hide 隐藏
func (e *EllipseShape) Hide() {
	e.Raster.Hide()
}

// NewEllipse 创建椭圆控件
func NewEllipse(name string, skinInfo widgetinterface.IWidgetSkin, rootNode widgetinterface.IWidgetItem) widgetinterface.IWidgetItem {
	if skinInfo == nil {
		return nil
	}

	ellipseShape := NewEllipseShape(color.RGBA{255, 255, 255, 255}, 100, 60)
	_ellipse := &Ellipse{
		EllipseShape: ellipseShape,
		Action:       &Action{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: canvas.NewRectangle(color.RGBA{0, 0, 0, 0}), // 初始化Border
		},
		rootNode: rootNode,

		// 默认值
		Width:       100,
		Height:      60,
		BorderWidth: 2,
		FillColor:   color.RGBA{255, 255, 255, 255},
		BorderColor: color.RGBA{0, 0, 0, 255},
	}

	// 从皮肤信息中读取属性
	_ellipse.loadFromSkin(skinInfo)

	_ellipse.EllipseShape.FillColor = _ellipse.FillColor
	_ellipse.EllipseShape.StrokeColor = _ellipse.BorderColor
	_ellipse.EllipseShape.StrokeWidth = _ellipse.BorderWidth

	// 设置椭圆尺寸
	_ellipse.EllipseShape.Resize(fyne.NewSize(_ellipse.Width, _ellipse.Height))

	// 初始化IUserInfo
	_ellipse.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), _ellipse)
	_ellipse.WidgetContainer.Container.IUserInfo = _ellipse.IUserInfo
	_ellipse.WidgetContainer.ac = _ellipse.Action
	_ellipse.WidgetContainer.SetObjects([]fyne.CanvasObject{_ellipse.EllipseShape})
	_ellipse.SetBase(_ellipse, rootNode)
	_ellipse.ExtendBaseWidget(_ellipse)

	return _ellipse
}

// loadFromSkin 从皮肤信息加载属性
func (e *Ellipse) loadFromSkin(skinInfo widgetinterface.IWidgetSkin) {
	attributes := skinInfo.GetAttribute()
	for _, attr := range attributes {
		switch attr.Key {
		case "Width":
			if val, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				e.Width = float32(val)
			}
		case "Height":
			if val, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				e.Height = float32(val)
			}
		case "BorderWidth":
			if val, err := strconv.ParseFloat(attr.Value, 32); err == nil {
				e.BorderWidth = float32(val)
			}
		case "FillColor":
			e.FillColor = e.parseColor(attr.Value)
		case "BorderColor":
			e.BorderColor = e.parseColor(attr.Value)
		}
	}
}

// parseColor 解析颜色字符串
func (e *Ellipse) parseColor(colorStr string) color.RGBA {
	parts := strings.Fields(colorStr)
	if len(parts) >= 3 {
		// 解析RGB值
		r, _ := strconv.Atoi(parts[0])
		g, _ := strconv.Atoi(parts[1])
		b, _ := strconv.Atoi(parts[2])
		a := 255 // 默认不透明

		// 如果有4个参数，第四个参数是alpha值
		if len(parts) == 4 {
			a, _ = strconv.Atoi(parts[3])
		}

		return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
	}
	return color.RGBA{255, 255, 255, 255} // 默认白色
}

// SetProperty 设置属性
func (e *Ellipse) SetProperty(key, value string, isProperty bool) {
	// 先处理椭圆特定属性
	switch key {
	case "Width":
		if val, err := strconv.ParseFloat(value, 32); err == nil {
			e.Width = float32(val)
			e.EllipseShape.Width = e.Width
			e.EllipseShape.Resize(fyne.NewSize(e.Width, e.Height))
			e.Refresh()
		}
	case "Height":
		if val, err := strconv.ParseFloat(value, 32); err == nil {
			e.Height = float32(val)
			e.EllipseShape.Height = e.Height
			e.EllipseShape.Resize(fyne.NewSize(e.Width, e.Height))
			e.Refresh()
		}
	case "BorderWidth":
		if val, err := strconv.ParseFloat(value, 32); err == nil {
			e.BorderWidth = float32(val)
			e.EllipseShape.StrokeWidth = e.BorderWidth
			e.Refresh()
		}
	case "FillColor":
		e.FillColor = e.parseColor(value)
		e.EllipseShape.FillColor = e.FillColor
		e.Refresh()
	case "BorderColor":
		e.BorderColor = e.parseColor(value)
		e.EllipseShape.StrokeColor = e.BorderColor
		e.Refresh()
	default:
		// 其他属性交给父类处理
		e.WidgetContainer.SetProperty(key, value, isProperty)
	}
}

// GetProperty 获取属性
func (e *Ellipse) GetProperty(key string) string {
	var result string
	e.IUserInfo.ForEachProperty(func(k, v string) {
		if k == key {
			result = v
		}
	})
	return result
}

// ForEachProperty 遍历属性
func (e *Ellipse) ForEachProperty(callback func(key, value string)) {
	e.IUserInfo.ForEachProperty(callback)
}

// GetSkinTempName 获取皮肤模板名称
func (e *Ellipse) GetSkinTempName() string {
	return e.IUserInfo.GetSkinTempName()
}

// SetSkinTempName 设置皮肤模板名称
func (e *Ellipse) SetSkinTempName(name string) {
	// 注意：IUserInfo接口可能没有SetSkinTempName方法
	// 这里只是占位符，实际实现可能需要通过其他方式
}

// MouseDown 鼠标按下事件
func (e *Ellipse) MouseDown(event *desktop.MouseEvent) {
	if e.Action != nil {
		e.Action.MouseDown(event)
	}
}

// MouseUp 鼠标抬起事件
func (e *Ellipse) MouseUp(event *desktop.MouseEvent) {
	if e.Action != nil {
		e.Action.MouseUp(event)
	}
}

// MouseMoved 鼠标移动事件
func (e *Ellipse) MouseMoved(event *desktop.MouseEvent) {
	if e.Action != nil {
		e.Action.MouseMoved(event)
	}
}

// CreateRenderer 创建渲染器
func (e *Ellipse) CreateRenderer() fyne.WidgetRenderer {
	return &ellipseRenderer{
		ellipse: e,
		shape:   e.EllipseShape,
	}
}

// ellipseRenderer 椭圆渲染器
type ellipseRenderer struct {
	ellipse *Ellipse
	shape   *EllipseShape
}

func (r *ellipseRenderer) Layout(size fyne.Size) {
	// 对于椭圆，我们需要确保宽高比正确
	r.shape.Resize(size)
	r.shape.Move(fyne.NewPos(0, 0))
}

func (r *ellipseRenderer) MinSize() fyne.Size {
	return fyne.NewSize(r.ellipse.Width, r.ellipse.Height)
}

func (r *ellipseRenderer) Refresh() {
	r.shape.Refresh()
}

func (r *ellipseRenderer) Objects() []fyne.CanvasObject {
	return []fyne.CanvasObject{r.shape}
}

func (r *ellipseRenderer) Destroy() {
}

// MinSize 最小尺寸
func (e *Ellipse) MinSize() fyne.Size {
	return fyne.NewSize(e.Width, e.Height)
}

// Position 获取位置
func (e *Ellipse) Position() fyne.Position {
	return e.WidgetContainer.Position()
}

// Size 获取尺寸
func (e *Ellipse) Size() fyne.Size {
	return fyne.NewSize(e.Width, e.Height)
}

// Hidden 是否隐藏
func (e *Ellipse) Hidden() bool {
	return !e.EllipseShape.Visible()
}

// SetVisible 设置可见性
func (e *Ellipse) SetVisible(visible bool) {
	if visible {
		e.EllipseShape.Show()
	} else {
		e.EllipseShape.Hide()
	}
}

// Show 显示控件
func (e *Ellipse) Show() {
	e.EllipseShape.Show()
}

// Hide 隐藏控件
func (e *Ellipse) Hide() {
	e.EllipseShape.Hide()
}

// Visible 是否可见
func (e *Ellipse) Visible() bool {
	return e.EllipseShape.Visible()
}

// Cursor 获取鼠标光标
func (e *Ellipse) Cursor() desktop.Cursor {
	return desktop.DefaultCursor
}

// SetCursor 设置鼠标光标
func (e *Ellipse) SetCursor(cursor desktop.Cursor) {
	// canvas.Circle 不支持直接设置光标
	// 这里只是占位符
}

// Move 移动控件位置
func (e *Ellipse) Move(pos fyne.Position) {
	e.WidgetContainer.Move(pos)
}

// Refresh 刷新控件
func (e *Ellipse) Refresh() {
	e.WidgetContainer.Refresh()
}
